import Vue from 'vue'
import VueRouter from 'vue-router'
// 引入仓库
import store from '../store'

function hasUrl(url, next) {
  let result = store.getters.getUser.menus_url.some(item => item == url)
  if (result) {
    next()
  } else {
    next('/home')
  }
}
Vue.use(VueRouter)
export const routesIndex = [
  {
    path: '/menu',
    component: () => import('../views/menu/menu.vue'),
    meta: {
      title: '菜单管理'
    },
    beforeEnter(to, from, next) {
      hasUrl('/menu', next)
    }
  },
  {
    path: '/role',
    component: () => import('../views/role/role.vue'),
    meta: {
      title: '角色管理'
    },
    beforeEnter(to, from, next) {
      hasUrl('/role', next)
    }
  },
  {
    path: "/administrator",
    component: () => import("../views/administrator/administrator.vue"),
    meta: {
      title: "管理员管理",
    },
    beforeEnter(to, from, next) {
      hasUrl('/administrator', next)
    }
  },
  {
    path: "/classify",
    component: () => import("../views/classify/classify.vue"),
    meta: {
      title: "商品分类管理",
    },
  
    beforeEnter(to, from, next) {
      hasUrl('/classify', next)
    }
  },
  {
    path: '/specification',
    component: () => import('../views/specification/specification.vue'),
    meta: {
      title: '商品规格管理'
    },
    beforeEnter(to, from, next) {
      hasUrl('/specification', next)
    }
  },
  {
    path: '/manage',
    component: () => import('../views/manage/manage.vue'),
    meta: {
      title: '商品管理'
    },
    beforeEnter(to, from, next) {
      hasUrl('/manage', next)
    }
  },
  {
    path: '/vip',
    component: () => import('../views/vip/vip.vue'),
    meta: {
      title: '会员列表'
    },
    beforeEnter(to, from, next) {
      hasUrl('/vip', next)
    }
  },
  {
    path: '/banner',
    component: () => import('../views/banner/banner.vue'),
    meta: {
      title: '轮播图列表'
    },
    beforeEnter(to, from, next) {
      hasUrl('/banner', next)
    }
  },
  {
    path: '/active',
    component: () => import('../views/active/active.vue'),
    meta: {
      title: '活动列表'
    },
    beforeEnter(to, from, next) {
      hasUrl('/active', next)
    }
  },
]
const routes = [
  {
    path: '/login',
    component: () => import('../pages/login.vue')
  },
  {
    path: '/index',
    component: () => import('../pages/index.vue'),
    children: [
      {
        path: '/home',
        component: () => import('../views/home.vue')
      },
      ...routesIndex,
      {
        path: '/menu',
        component: () => import('../views/menu/menu.vue'),
        meta: {
          title: '菜单管理'
        }
      },
      {
        path: '/administrator',
        component: () => import('../views/administrator/administrator.vue'),
        meta: {
          title: '管理员管理'
        }
      },
      {
        path: '/role',
        component: () => import('../views/role/role'),
        meta: {
          title: '角色管理'
        }
      },
      {
        path: '/banner',
        component: () => import('../views/banner/banner.vue'),
        meta: {
          title: '轮播图列表'
        }
      },
      {
        path: '/active',
        component: () => import('../views/active/active.vue'),
        meta: {
          title: '活动列表'
        }
      },
      {
        path: '/classify',
        component: () => import('../views/classify/classify.vue'),
        meta: {
          title: '商品分类'
        }
      },
      {
        path: '/manage',
        component: () => import('../views/manage/manage.vue'),
        meta: {
          title: '商品管理'
        }
      },
      {
        path: '/specification',
        component: () => import('../views/specification/specification.vue'),
        meta: {
          title: '商品规格管理'
        }
      },
      {
        path: '/vip',
        component: () => import('../views/vip/vip.vue'),
        meta: {
          title: '会员列表'
        }
      },
      {
        path: '',
        redirect: '/home'
      }
    ]
  },
  {
    // 一级路由重定向
    path: '*',
    redirect: '/index'
  }
]

const router = new VueRouter({
  routes,
  mode: 'history',
  base: process.env.BASE_URL,
})

//调用全局前置导航守卫
router.beforeEach((to, from, next) => {
  if (to.path == '/login') {
    next()
    return
  }
  if (store.getters.getUser) {
    next()
    return
  }
  next('/login')
})


export default router
